<?php



namespace app\admin\model\tongcheng;

use addons\tongcheng\enum\Gender;
use addons\tongcheng\enum\RiderEnterStatus;
use addons\tongcheng\enum\RiderWorkStatus;
use addons\tongcheng\enum\UserRole;
use addons\tongcheng\model\BaseModel;
use addons\tongcheng\model\UserModel;
use addons\tongcheng\model\UserInfoModel;
use app\common\library\Auth;
use fast\Random;
use traits\model\SoftDelete;

class RiderEnterModel extends BaseModel
{

    use SoftDelete;


    // 表名
    protected $name = 'tongcheng_rider';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];
 
    public function getGenderList()
    {
        return ['1' => __('Gender 1'), '0' => __('Gender 0')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /**
     * 创建司机信息
     * @param $rider_enter
     * @return RiderModel
     */
    public static function createDrive($rider_enter)
    {   
        
     
      
        $drive = RiderModel::get([
            // 'zone_id' => $rider_enter['zone_id'],
            'user_id' => $rider_enter['user_id'],
        ]);
      
        // 创建司机信息
        if (!$drive) {
            $drive = RiderModel::create([
                
                'zone_id'                => 0,
                'user_id'                => $rider_enter['user_id'],
                'real_name'              => $rider_enter['real_name'],
              
                'work_status'            => RiderWorkStatus::WORKING,
                'status'                 => 1,
                'day_order_limit'        => 0,
                'taking_order_limit'     => 0,
                'today_order_count'      => 0,
                'taking_order_count'     => 0,
                
            ]);
        }

       $drive_info = UserInfoModel::get([
            // 'zone_id' => $rider_enter['zone_id'],
            'user_id' => $rider_enter['user_id'],
        ]);
        
       if(!$drive_info){
           UserInfoModel::create([
               'user_id'                 => $rider_enter['user_id'],
               'role'                    => UserRole::DRIVE,
               
        ]);
       }
   

        return $drive;
    }
}
